package com.xinlianshiye.im.service.impl;

import com.xinlianshiye.im.RequestEnum;
import com.xinlianshiye.im.dao.FriendApplicationDao;
import com.xinlianshiye.im.dao.FriendDao;
import com.xinlianshiye.im.bean.Friend;
import com.xinlianshiye.im.bean.FriendApplication;
import com.xinlianshiye.im.bean.User;
import com.xinlianshiye.im.bean.UserGroup;
import com.xinlianshiye.im.service.FriendService;
import com.xinlianshiye.im.service.RecordService;
import com.xinlianshiye.im.service.UserGroupService;
import com.xinlianshiye.im.service.UserService;
import com.xinlianshiye.im.response.UserViews;
import com.xinlianshiye.clouds.common.exception.WebMessageExceptionBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class FriendServiceImpl implements FriendService {

    private final FriendDao friendDao;
    private final FriendApplicationDao friendApplicationDao;
    private final WebMessageExceptionBuilder exceptionBuilder;

    public FriendServiceImpl(FriendDao friendDao, FriendApplicationDao friendApplicationDao, WebMessageExceptionBuilder exceptionBuilder) {
        this.friendDao = friendDao;
        this.friendApplicationDao = friendApplicationDao;
        this.exceptionBuilder = exceptionBuilder;
    }

    @Autowired
    private RecordService recordService;
    @Autowired
    private UserService userService;
    @Autowired
    private UserGroupService userGroupService;

    @Override
    public void create(FriendApplication friendApplication) {
        // 给双方在好友表中添加对应的记录
        if (friendApplication.getApplyState() == FriendApplication.ApplyStateEnum.Agree) {
            User targetUser = friendApplication.getTargetUser();
            User user = friendApplication.getUser();
            if (friendDao.existsByUserAndFriend(user, targetUser)) {
                throw exceptionBuilder.build(RequestEnum.FRIENDALREADY.getCode(), RequestEnum.FRIENDALREADY.getMessage());
            }
            Friend friend = new Friend();
            friend.setUser(targetUser);
            friend.setFriend(user);
            UserGroup fromGroup = userGroupService.getOrCreateDefaultGroup(targetUser);
            friend.setUserGroupId(fromGroup.getId());
            friendDao.save(friend);
            userService.sendInit(targetUser.getId());

            Friend friend2 = new Friend();
            friend2.setUser(user);
            friend2.setFriend(targetUser);
            UserGroup toGroup = userGroupService.getOrElseThrow(friendApplication.getGroup().getId());
            friend2.setUserGroupId(toGroup.getId());
            friendDao.save(friend2);
            userService.sendInit(user.getId());
        }
    }

    @Override
    public List<User> searchFriendByName(Integer userId, String name) {
        return friendDao.findFriendByName(userId, name);
    }

    @Override
    public List<UserViews> listNotFriend(Integer userId) {
        return friendDao.findNotFriend(userId).stream().map(other -> {
            UserViews uv = new UserViews();
            uv.setId(other.getId());
            uv.setUsername(other.getUsername());
            //判断用户是否以及申请过了
            uv.setStatus(friendApplicationDao.existsByUserIdAndTargetUserIdAndApplyState(userId, other.getId(), FriendApplication.ApplyStateEnum.Pending) ? 1 : 0);
            return uv;
        }).collect(Collectors.toList());
    }

    @Override
    public void delete(Integer userId, Integer friendId) {
        Friend friend = friendDao.findByUserIdAndFriendId(userId, friendId);
        // 单方面删除和好友的聊天记录
        recordService.deleteByFriend(friend);
        // 删除好友申请
        friendApplicationDao.deleteByUserIdAndTargetUserId(userId, friendId);
        friendApplicationDao.deleteByUserIdAndTargetUserId(friendId, userId);
        // 删除好友关系
        friendDao.deleteByUserIdAndFriendId(userId, friendId);
        friendDao.deleteByUserIdAndFriendId(friendId, userId);
        userService.sendInit(friendId);
    }
}
